
var app = getApp()
var getAppGlobalData = require('../../../../templates/copyright/copyright.js');
Page({
  data: {
    dataList: [],
    globalData: {},
    status: '',
    currentIndex: 0,
    tempFilePaths: [],
    tempFileImgs: []
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数 
    var that = this;
    getAppGlobalData.getAppGlobalData(that);

    console.log(options,"//////////***************")
    if (options.status) {
      that.setData({
        status: options.status
      })
      if (options.status == 'news') {
        wx.setNavigationBarTitle({
          title: '动态发布'
        })
      } else if (options.status == 'code') {
        wx.setNavigationBarTitle({
          title: '自定义码'
        })
      }
    }
  },
  onReady: function () {
    // console.log("页面渲染完成")
  },
  onShow: function () {
    // 页面显示 
  },
  onHide: function () {
    // console.log("页面隐藏")
  },
  onUnload: function () {
    // console.log("页面关闭")
  },
  onPullDownRefresh: function () {
    // console.log("监听用户下拉动作") 
    var that = this;
    that.setData({
      dataList: [],
      page: 1,
      more: true,
      loading: false,
      isEmpty: false,
      show: false,
    })
    that.getListData();
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1000);
  },
  onReachBottom: function () {
    // console.log("监听页面上拉触底") 
    var that = this;
    that.setData({
      show: true
    })
    if (that.data.isEmpty == false) {
      that.setData({
        page: that.data.page + 1
      })
      that.getListData();
    }
  },
  onPageScroll: function (e) {
    //console.log("监听页面滚动", e);
  },
  onShareAppMessage: function (res) {
    // console.log("用户点击右上角分_享")
  },
  chooseImage: function (e) {
    var that = this;
    wx.showActionSheet({
      itemList: ['优雅自拍', '相册收藏'],
      itemColor: '#3675f1',
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImageShop('camera');
          } else if (res.tapIndex == 1) {
            that.chooseWxImageShop('album');
          }
        }
      }
    });
  },
  chooseWxImageShop: function (type) {
    var that = this;
    var tmpPath = that.data.tempFilePaths;
    var tmpImg = that.data.tempFileImgs;
    wx.chooseImage({
      count: 8,
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        console.log(res, "===========================  res****")
        for (let i in res.tempFilePaths) {
          wx.showLoading({
            title:'图片上传中...'
          });
          var uploadUrl = app.util.url('entry/wxapp/upload');
          var nowPage = getCurrentPages();
          if (nowPage.length) {
            nowPage = nowPage[getCurrentPages().length - 1];
            if (nowPage && nowPage.__route__) { 
              uploadUrl = uploadUrl + '&m=' + nowPage.__route__.split('/')[0];
            }
          }
          console.log(res.tempFilePaths[i], "res.tempFilePaths[i]")
          wx.uploadFile({
            url: uploadUrl,
            filePath: res.tempFilePaths[i],
            name: 'upfile',
            formData: {},
            success: function (res) {
              console.log(res, "*********uploadFile res")
              wx.hideLoading();
              var tmpData = JSON.parse(res.data);
              tmpPath.push(tmpData.data.path);
              tmpImg.push(tmpData.data.img);
              if (tmpPath.length > 8) {
                wx.showModal({
                  title: '',
                  content: '最多只能上传8张图片哦！',
                  confirmText: '知道啦',
                  showCancel: false,
                  success: res => {
                    if (res.confirm) {
                    } else {
                    }
                  }
                });
                return false;
              }
              that.setData({
                tempFilePaths: tmpPath,
                tempFileImgs: tmpImg,
              })
            }
          })
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  toAddNews: function (content) {
    var that = this;
    var tmpImg = that.data.tempFileImgs;
    var tmpCover = '';
    for (let i in tmpImg) {
      tmpCover += (tmpImg[i] + ',')
    }
    tmpCover = tmpCover.slice(0, -1);
    app.util.request({
      'url': 'entry/wxapp/releaseTimeline',
      'cachetime': '30',
      'method': 'POST',
      'data': {
        title: content,
        cover: tmpCover
      },
      success: function (res) {
        // console.log("entry/wxapp/releaseTimeline ==>", res)
        if (!res.data.errno) {
          wx.showModal({
            title: '',
            content: '发布成功！',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack();
              }
            }
          })
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  toReleaseQr: function (title, content) {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/releaseQr',
      'cachetime': '30',
      'method': 'POST',
      'data': {
        title: title,
        content: content
      },
      success: function (res) {
        // console.log("entry/wxapp/releaseQr ==>", res)
        if (!res.data.errno) {
          wx.navigateBack();
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  toJump: function (e) {
    var that = this;
    var status = e.currentTarget.dataset.status;
    var index = e.currentTarget.dataset.index;
    if (status == 'toShowDelete') {
      console.log("显示删除图片按钮")
      that.setData({
        currentIndex: index
      })
    } else if (status == 'toDeleteImg') {
      console.log("删除图片")
      let tmpData = that.data.tempFilePaths;
      let tmpUpData = that.data.tempFileImgs;
      tmpUpData.splice(index, 1);
      tmpData.splice(index, 1);
      that.setData({
        tempFilePaths: tmpData,
        tempFileImgs: tmpUpData
      })
    }
  },
  formSubmit: function (e) {
    var that = this;
    console.log(e, "eeeeeeee///////////")
    var formId = e.detail.formId;
    var status = e.detail.target.dataset.status;
    var title = e.detail.value.title;
    var content = e.detail.value.content;
    that.toSaveFormIds(formId);
    if (status == 'toAddNews') {
      console.log("确定发布")


      if (!content) {
        var message = '';
        if (that.data.status == 'news') {
          message = '请填写动态信息！'
        } else if (that.data.status == 'code') {
          message = '请填写自定义码信息！'
        }
        wx.showModal({
          title: '',
          content: message,
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
            }
          }
        })
        return false;
      }


      if (that.data.status == 'news') {
        console.log("新建动态")
        // var tmpImg = that.data.tempFileImgs;
        // if (tmpImg.length < 1) {
        //   wx.showModal({
        //     title: '',
        //     content: '尚未选择图片，请选择图片！',
        //     showCancel: false,
        //     success: function (res) {
        //       if (res.confirm) {
        //       }
        //     }
        //   })
        //   return false;
        // }

        that.toAddNews(content);
      } else if (that.data.status == 'code') {
        console.log("自定义码")
        if (!title) { 
          wx.showModal({
            title: '',
            content: '请填写自定义码标题！',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
              }
            }
          })
          return false;
        }
        that.toReleaseQr(title, content);
      }
    }
  },
  toSaveFormIds: function (formId) {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/formid',
      'cachetime': '30',
      'showLoading': false,
      'method': 'POST',
      'data': {
        formId: formId
      },
      success: function (res) {
        // console.log("entry/wxapp/formid ==>", res)
        if (!res.data.errno) {
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  }
})